package com.example.doctorsystem.dao;

import com.example.doctorsystem.model.Medicine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class MedicineDaoImpl implements MedicineDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private RowMapper<Medicine> rowMapper = new RowMapper<>() {
        @Override
        public Medicine mapRow(ResultSet rs, int rowNum) throws SQLException {
            Medicine m = new Medicine();
            m.setId(rs.getInt("id"));
            m.setName(rs.getString("name"));
            m.setDescription(rs.getString("description"));
            m.setQuantity(rs.getInt("quantity"));
            m.setPrice(rs.getBigDecimal("price"));
            m.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime());
            return m;
        }
    };

    @Override
    public int addMedicine(Medicine medicine) {
        String sql = "INSERT INTO medicines(name, description, quantity, price) VALUES (?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                medicine.getName(),
                medicine.getDescription(),
                medicine.getQuantity(),
                medicine.getPrice());
    }

    @Override
    public List<Medicine> getAllMedicines() {
        String sql = "SELECT * FROM medicines ORDER BY create_time DESC";
        return jdbcTemplate.query(sql, rowMapper);
    }

    @Override
    public Medicine getMedicineById(int id) {
        String sql = "SELECT * FROM medicines WHERE id = ?";
        List<Medicine> list = jdbcTemplate.query(sql, rowMapper, id);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public int updateMedicine(Medicine medicine) {
        String sql = "UPDATE medicines SET name = ?, description = ?, quantity = ?, price = ? WHERE id = ?";
        return jdbcTemplate.update(sql,
                medicine.getName(),
                medicine.getDescription(),
                medicine.getQuantity(),
                medicine.getPrice(),
                medicine.getId());
    }

    @Override
    public int deleteMedicineById(int id) {
        String sql = "DELETE FROM medicines WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
    @Override
    public Medicine getMedicineByName(String name) {
        String sql = "SELECT * FROM medicines WHERE name = ?";
        List<Medicine> list = jdbcTemplate.query(sql, (rs, rowNum) -> {
            Medicine medicine = new Medicine();
            medicine.setId(rs.getInt("id"));
            medicine.setName(rs.getString("name"));
            medicine.setQuantity(rs.getInt("quantity"));
            return medicine;
        }, name);
        return list.isEmpty() ? null : list.get(0);
    }
    @Override
    public int updateQuantity(int medicineId, int newQuantity) {
        String sql = "UPDATE medicines SET quantity = ? WHERE id = ?";
        return jdbcTemplate.update(sql, newQuantity, medicineId);
    }


}

